import IButtonOptions from "@/components/ButtonGroup/button-scheme";
import {listAllDirectory} from '@/api/sysmenuApi'
import {IFormItem,IFormConfig,ISelectOption} from '@/components/Form/form-scheme'
import InputType from '@/enum/InputType'

let directories:ISelectOption[] = []
let commonYesNo:ISelectOption[] = []
let type:ISelectOption[] = []


/**
 * 查询表单配置
 */
const searchItems: IFormItem[] = [
    {
        model: "parentId",
        type: InputType.SELECT,
        placeholder: '-- 请选择 --',
        label: `父菜单:`,
        clearable: true,
        options: directories
    },
    {
        model: 'name',
        type: InputType.INPUT,
        label: `名称:`,
        clearable: true
    },
    {
        model: 'type',
        type: InputType.SELECT,
        placeholder: '-- 请选择 --',
        label: `类型:`,
        clearable: true,
        options: type
    },
    {
        model: 'isLink',
        type: InputType.SELECT,
        placeholder: '-- 请选择 --',
        label: `外链:`,
        clearable: true,
        options: commonYesNo
    },
]
const SearchConfig: IFormConfig = {
    items: searchItems,
    labelWidth: "80px",
    span: 8
}
export {SearchConfig}


/**
 * 按钮组配置
 */
const ButtonConfig: IButtonOptions = {
    reset: {
        clickCall: 'reset',
        btnTip: '重置'
    },
    query: {
        clickCall: 'query',
        btnTip: '查询'
    },
    add: {
        clickCall: 'openDialog',
        btnTip: '添加'
    },
    edit: {
        clickCall: 'openDialog',
        btnTip: '编辑'
    },
    delete: {
        clickCall: 'openDeleteDialog',
        btnTip: '删除'
    }
}
export  {ButtonConfig}


const getAllDirectory = async () => {
    const res = await listAllDirectory()
    if (res instanceof Array) {
        let temp
        res.forEach(item => {
            temp = {
                title: item.name,
                value: item.menuId
            }
            if (directories.indexOf(temp) === -1) {
                directories.push(temp)
            }
        })
    }
}

const init = (dicts) => {
    let yesNo = dicts['common_yes_no']
    let menuType = dicts['sys_menu_type']
    if (yesNo && yesNo instanceof Array) {
        let item
        yesNo.forEach(item => {
             item = {
                 title: item.label,
                 value: item.value
             }
             if (commonYesNo.indexOf(item) === -1) {
                 commonYesNo.push(item)
             }
        })
    }
    if (menuType && menuType instanceof Array) {
        let item
        menuType.forEach(item => {
             item = {
                 title: item.label,
                 value: item.value
             }
             if (type.indexOf(item) === -1) {
                 type.push(item)
             }
        })
    }
}




export {getAllDirectory,init}